package org.example.infrastructure.persistence.user.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Interfaces.dto.UserQueryDto;
import org.example.domain.user.entity.User;
import org.example.domain.user.repository.UserRepository;
import org.example.infrastructure.persistence.user.po.UserPO;
import org.example.model.PageInfo;
import org.example.util.ModelMapperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author example
 * @since 2024-08-04
 */
@Repository
public class UserRepositoryImpl extends ServiceImpl<UserMapper, UserPO> implements UserRepository {

    private final static Logger logger= LoggerFactory.getLogger(UserRepositoryImpl.class);
    @Override
    public List<User> findAll() {
        List<UserPO> list= baseMapper.selectList(null);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.stream().map(o->ModelMapperUtil.mapToModel(o,User.class)).collect(Collectors.toList());
    }

    @Override
    public User findById(Long id) {
        UserPO po= baseMapper.selectById(id);
        if(null!=po){
            return ModelMapperUtil.mapToModel(po,User.class);
        }
        return null;
    }

    @Override
    public boolean save(User entity) {
        UserPO po= ModelMapperUtil.mapToPo(entity,UserPO.class);
        int result= baseMapper.insert(po);
        entity.setId(po.getId());
        return result>0;
    }

    @Override
    public void update(User entity) {
        UserPO po= ModelMapperUtil.mapToPo(entity,UserPO.class);
        baseMapper.updateById(po);
    }

    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<User> listByName(String name) {
        List<UserPO> list=((UserMapper)baseMapper).listByName(name);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.stream().map(o->ModelMapperUtil.mapToModel(o,User.class)).collect(Collectors.toList());
    }

    @Override
    public List<User> queryUsers(UserQueryDto userQueryDto) {

        QueryWrapper<UserPO> queryWrapper=buildQuery(userQueryDto);
        List<UserPO> list= baseMapper.selectList(queryWrapper);

        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.stream().map(o->ModelMapperUtil.mapToModel(o,User.class)).collect(Collectors.toList());
    }

    @Override
    public PageInfo<User> listByPage(UserQueryDto userQueryDto) {
        IPage<UserPO> page=new PageDTO<>(userQueryDto.getPageNum(), userQueryDto.getPageSize());
        QueryWrapper<UserPO> queryWrapper=buildQuery(userQueryDto);

        // 执行查询
        IPage<UserPO> userPage = baseMapper.selectPage(page,queryWrapper); // 请替换为你实际使用的查询方法
        logger.info("用户记录量：{}",userPage.getTotal());

        PageInfo<User> result=ModelMapperUtil
                .mapToPage((List)userPage.getRecords(),userPage.getCurrent(),userPage.getSize(),userPage.getTotal(),User.class);

        return result;
    }

    /**
     * 构建查询
     * @param userQueryDto
     * @return
     */
    private QueryWrapper<UserPO> buildQuery(UserQueryDto userQueryDto){
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();

        // 添加名称查询条件
        if (userQueryDto.getName() != null) {
            queryWrapper.eq("name", userQueryDto.getName());
        }

        // 添加出生日期范围查询条件
        if (userQueryDto.getBirthDateRange() != null && userQueryDto.getBirthDateRange().size() == 2) {
            LocalDate startDate = userQueryDto.getBirthDateRange().get(0);
            LocalDate endDate = userQueryDto.getBirthDateRange().get(1);
            queryWrapper.between("birthday", startDate, endDate);
        }
        return queryWrapper;
    }
}
